$().ready(function() {
    validateRule();
    if($("#giftCnt").val() != 0){
        $('.table-striped').show();
    }
});

$.validator.setDefaults({
    submitHandler : function() {
        save();
    }
});
function save() {
    var gitList = new Array();
    var buyNums = '';
    var giftNums = '';
    $("#t_body tr").each(function(i){
        var buyNum = $(this).children("td").eq(0).html();
        var giftNum = $(this).children("td").eq(1).html();
        if(buyNums == ''){
            buyNums = buyNum;
        }else{
            buyNums = buyNums +','+ buyNum;
        }
        if(giftNums == ''){
            giftNums = giftNum;
        }else{
            giftNums = giftNums +','+ giftNum;
        }
    });
    $("#buyNums").val(buyNums);
    $("#giftNums").val(giftNums);
    $.ajax({
        cache : true,
        type : "POST",
        url : "/ticket/update",
        data : $('#signupForm').serialize(),// 你的formid
        async : false,
        error : function(request) {
            parent.layer.alert("服务器异常，请联系管理员");
        },
        success : function(data) {
            if (data.code == 0) {
                parent.layer.msg("编辑停车券成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
            } else {
                parent.layer.alert(data.msg)
            }
        }
    });

}
jQuery.validator.addMethod("minNumber",function(value, element){
    var returnVal = true;
    inputZ=value;
    var ArrMen= inputZ.split(".");    //截取字符串
    if(ArrMen.length==2){
        if(ArrMen[1].length>2){    //判断小数点后面的字符串长度
            returnVal = false;
            return false;
        }
    }
    return returnVal;
},"小数点后最多为两位");         //验证错误信息
jQuery.validator.addMethod("checkName",function(value, element){
    var returnVal = true;
    $.ajax({
        cache : true,
        type : "POST",
        url : "/ticket/check",
        data : $('#signupForm').serialize(),// 你的formid
        async : false,
        error : function(request) {

        },
        success : function(data) {
            if (data.code != 0) {
                returnVal = false;
            }
        }
    });
    return returnVal;
},"停车券名称重复了，请重新输入");         //验证错误信息
function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules : {
            price : {
                required : true,
                number : true,
                min:0,
                max:9999,
                minNumber: $("#price").val()
            },
            minute : {
                required : true,
                digits:true,
                min:1,
                max:9999
            },
            ticketName : {
                required : true,
                checkName : $("#ticketName").val(),
                maxlength:50
            }
        },
        messages : {
            price : {
                required : icon + "价格必填且必须是数字",
                minNumber: icon +"价格最多精确到小数点后两位"
            },
            minute : {
                required : icon + "抵用时长必填"
            },
            ticketName : {
                required : icon + "停车券名称必填",
                checkName : icon + "停车券名称重复，请重新输入",
                maxlength : icon + "停车券名称最大长度50个字符"
            }
        }
    })
}

function setGift(buyNum,giftNum){
    $(".table-striped").show();
    var segHtml = $("#t_body").html();
    segHtml = segHtml + "<tr class='giftclass'><td>"+buyNum+"</td><td>"+giftNum+"</td><td style='text-align:right'><a class='btn btn-warning'  href='javascript:;'  onclick='delOne(this)' title='删除'><i class='fa fa-remove'></i></a></td></tr>";
    $("#t_body").html(segHtml);
}

function delOne(obj){
    $(obj).parent().parent().remove();
    if($(".giftclass").length ==0){
        $(".table-striped").hide();
    }
}

function clearAll(){
    $("#t_body").html('');
}

function addGift(){
    if($(".giftclass").length > 4){
        parent.layer.msg("一张停车券最多添加5条优惠规则");
        return;
    }
    var buyNums = '-1';
    $("#t_body tr").each(function(i){
        var buyNum  = $(this).children("td").eq(0).html();
        if(buyNums == ''){
            buyNums = buyNum;
        }else{
            buyNums = buyNums +','+ buyNum;
        }
    });
    layer.open({
        type : 2,
        title : '添加优惠信息',
        maxmin : true,
        shadeClose : false, // 点击遮罩关闭层
        offset:'20px',
        area : [ '520px', '320px' ],
        content :  '/ticket/giftadd/'+buyNums// iframe的url
    });
}
